package cn.tedu.property.management.service.impl;

import cn.tedu.property.management.ex.ServiceException;
import cn.tedu.property.management.mapper.MaintenanceBusinessMapper;
import cn.tedu.property.management.pojo.dto.MaintenanceBusinessAddNewDTO;
import cn.tedu.property.management.pojo.dto.OrderUpdateDTO;
import cn.tedu.property.management.pojo.vo.OrderListItemVO;
import cn.tedu.property.management.pojo.vo.OrderStandardVO;
import cn.tedu.property.management.pojo.entity.WorkOrder;
import cn.tedu.property.management.service.IMaintenanceService;
import cn.tedu.property.management.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 订单业务的实现类
 */
@Slf4j
@Service
public class MaintenanceServiceImpl implements IMaintenanceService {

    @Autowired
    private MaintenanceBusinessMapper businessMapper;

    /**
     * 添加新的维修单
     * @param businessAddNewDTO
     */
    @Override
    public void addNew(MaintenanceBusinessAddNewDTO businessAddNewDTO) {
        WorkOrder order = new WorkOrder();
        BeanUtils.copyProperties(businessAddNewDTO,order);
        int addNew = businessMapper.addNew(order);
        if (addNew != 1) {
            String message = "上报订单失败,服务器异常,请稍后尝试";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    /**
     * 根据id删除维修单
     * @param id
     */
    @Override
    public void deleteByOrderId(Long id) {
        OrderStandardVO details = businessMapper.getOrderDetailsById(id);
        if (details == null) {
            String message = "删除失败,该维修订单不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int delete = businessMapper.deleteByOrderId(id);
        if (delete != 1) {
            String message = "删除失败,服务器异常,请稍后尝试";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    /**
     * 更新维修单信息
     * @param id
     * @param orderUpdateDTO
     */
    @Override
    public void updateOrder(Long id, OrderUpdateDTO orderUpdateDTO) {
        OrderStandardVO details = businessMapper.getOrderDetailsById(id);
        if (details == null) {
            String message = "修改失败,该维修订单不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        WorkOrder workOrder = new WorkOrder();
        BeanUtils.copyProperties(orderUpdateDTO,workOrder);
        workOrder.setId(id);
        int i = businessMapper.updateOrder(workOrder);
        if (i != 1){
            String message = "修改失败,服务器异常,请稍后尝试";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    /**
     * 根据维修单id查询维修单详情
     * @param id
     * @return
     */
    @Override
    public OrderStandardVO getOrderDetailsById(Long id) {
        OrderStandardVO details = businessMapper.getOrderDetailsById(id);
        if (details == null) {
            String message = "查询失败,该维修订单不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return details;
    }

    /**
     * 根据业主姓名获取维修单详情
     * @param name
     * @return
     */
    @Override
    public OrderStandardVO getOrderDetailsByOwnerName(String name) {
        OrderStandardVO details = businessMapper.getOrderDetailsByOwnerName(name);
        if (details == null) {
            String message = "查询失败,该维修订单不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return details;
    }

    /**
     * 查询所有维修订单
     * @return
     */
    @Override
    public List<OrderListItemVO> getAll() {
        List<OrderListItemVO> orders = businessMapper.getAll();
        return orders;
    }
}
